/**
 * OWASP GoatDroid Project
 * 
 * This file is part of the Open Web Application Security Project (OWASP)
 * GoatDroid project. For details, please see
 * https://www.owasp.org/index.php/Projects/OWASP_GoatDroid_Project
 *
 * Copyright (c) 2011 - The OWASP Foundation
 * 
 * GoatDroid is published by OWASP under the GPLv3 license. You should read and accept the
 * LICENSE before you use, modify, and/or redistribute this software.
 * 
 * @author Jack Mannino, nVisium Security (https://www.nvisiumsecurity.com)
 * @created 2011
 */
package org.owasp.goatdroid.gui.emulator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.net.SocketException;
import java.util.ArrayList;
import javax.swing.JTree;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.net.telnet.TelnetClient;
import org.owasp.goatdroid.gui.LessonTreeBuilder;
import org.owasp.goatdroid.gui.config.Config;
import org.owasp.goatdroid.gui.exception.CorruptConfigException;

public class Emulator {

	static String sdkPath;
	static Config config;
	static String slash;

	public Emulator() throws FileNotFoundException, IOException,
			CorruptConfigException {
		config = new Config();
		sdkPath = Config.getSDKPath();
		if (System.getProperty("os.name").toLowerCase().startsWith("windows"))
			slash = File.separator + File.separator;
		else
			slash = File.separator;
	}

	static public String getAppPath(JTree jTree) {
		LessonTreeBuilder lessonTree = new LessonTreeBuilder();

		String directoryPath = lessonTree.getCurrentNodeParent(jTree) + slash
				+ "android_app";
		File directory = new File(directoryPath);

		try {
			File[] entries = directory.listFiles();
			// Go over entries
			for (File entry : entries) {
				if (entry.isFile() && entry.toString().endsWith(".apk")) {
					return entry.toString();
				}
			}
		} catch (NullPointerException e) {
			e.getMessage();
		}
		// If we get here, we lost
		return "";
	}

	static public void sendLocation(String latitude, String longitude)
			throws SocketException, IOException {

		TelnetClient telnet = new TelnetClient();
		telnet.setDefaultPort(5554);
		telnet.connect("localhost");
		PrintStream outStream = new PrintStream(telnet.getOutputStream());
		// geo fix takes longitude/latitude, in that order
		outStream.println("geo fix " + longitude + " " + latitude);
		outStream.flush();
	}

	static public void pushAppOntoDevice(String appPath) {

		CommandLine cmdLine = new CommandLine(sdkPath + slash
				+ "platform-tools" + slash + "adb");
		cmdLine.addArgument("install");
		cmdLine.addArgument(appPath, false);
		DefaultExecutor executor = new DefaultExecutor();
		try {
			int exitValue = executor.execute(cmdLine);
			exitValue += 0;
		} catch (ExecuteException e) {
			e.getMessage();
		} catch (IOException e) {
			e.getMessage();
		}
	}

	static public ArrayList<String> getVirtualDevices(String virtualDevicePath) {

		ArrayList<String> virtualDevices = new ArrayList<String>();
		File deviceFolder = new File(virtualDevicePath);

		for (File device : deviceFolder.listFiles()) {
			if (device.toString().endsWith(".avd") && device.isDirectory()) {
				if (device.toString().contains(File.separator)) {
					String[] splitDeviceString = device.toString().split(
							File.separator + File.separator);
					virtualDevices
							.add(splitDeviceString[splitDeviceString.length - 1]);
				} else
					virtualDevices.add(device.toString());
			}
		}
		return virtualDevices;
	}
}
